﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;

namespace SurfaceInteraction.UI.WP7
{
    public class Configuration
    {
        private const string REMOTE_DESKTOP_SERVICE_URL_SETTING_NAME = "RDServiceURL";
        private const string FILE_EXCHANGE_SERVICE_URL_SETTING_NAME = "FEServiceURL";
        private const string ID__SETTING_NAME = "Id";
        public const string REMOTE_DESKTOP_SERVICE_EP = "RemoteDesktopServiceEP";
        public const string FILE_EXCHANGE_SERVICE_EP = "FileExchangeEP";
        private const string DEFAULT_REMOTE_DESKTOP_SERVICE_URL = "http://localhost:9102/";
        private const string DEFAULT_FILE_EXCHANGE_SERVICE_URL = "http://localhost:9103/";

        private static byte _id = 0;
        private static string _remoteDesktop_ServiceURL = DEFAULT_REMOTE_DESKTOP_SERVICE_URL;
        private static string _fileExchange_ServiceURL = DEFAULT_FILE_EXCHANGE_SERVICE_URL;

        public static byte Id
        {
            get
            {
                if (_id == 0)
                {
                    if (TryGetSetting<byte>(ID__SETTING_NAME, out _id) == false)
                        _id = 0;
                }
                return _id;
            }
            set
            {
                _id = value;
                StoreSetting<byte>(ID__SETTING_NAME, _id);
            }
        }

        public static int IdleTimeRemoteDesktopInMilliSeconds
        {
            get { return 250; }
        }

        public static int DownloadTimerInMilliSeconds
        {
            get { return 250; }
        }

        public static string RemoteDesktop_ServiceURL
        {
            get
            {
                if (_remoteDesktop_ServiceURL == DEFAULT_REMOTE_DESKTOP_SERVICE_URL)
                {
                    if (TryGetSetting<string>(REMOTE_DESKTOP_SERVICE_URL_SETTING_NAME, out _remoteDesktop_ServiceURL) == false)
                        _remoteDesktop_ServiceURL = DEFAULT_REMOTE_DESKTOP_SERVICE_URL;
                }
                return _remoteDesktop_ServiceURL;
            }

            set
            {
                string val = string.Format("http://{0}:9102/", value);
                //string val = string.Format("http://{0}", value);
                StoreSetting<string>(REMOTE_DESKTOP_SERVICE_URL_SETTING_NAME, val);
                _remoteDesktop_ServiceURL = val;
            }
        }

        public static string FileExchange_ServiceURL
        {
            get
            {
                if (_fileExchange_ServiceURL == DEFAULT_FILE_EXCHANGE_SERVICE_URL)
                {
                    if (TryGetSetting<string>(FILE_EXCHANGE_SERVICE_URL_SETTING_NAME, out _fileExchange_ServiceURL) == false)
                        _fileExchange_ServiceURL = DEFAULT_FILE_EXCHANGE_SERVICE_URL;
                }
                return _fileExchange_ServiceURL;
            }

            set
            {
                string val = string.Format("http://{0}:9103/", value);
                //string val = string.Format("http://{0}", value);
                StoreSetting<string>(FILE_EXCHANGE_SERVICE_URL_SETTING_NAME, val);
                _fileExchange_ServiceURL = val;
            }
        }

        private static IsolatedStorageSettings Settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;

        public static void StoreSetting(string settingName, string value)
        {
            StoreSetting<string>(settingName, value);
        }

        public static void StoreSetting<TValue>(string settingName, TValue value)
        {
            if (!Settings.Contains(settingName))
                Settings.Add(settingName, value);
            else
                Settings[settingName] = value;

            // EDIT: if you don't call Save then WP7 will corrupt your memory!
            Settings.Save();
        }

        public static bool TryGetSetting<TValue>(string settingName, out TValue value)
        {
            if (Settings.Contains(settingName))
            {
                value = (TValue)Settings[settingName];
                return true;
            }

            value = default(TValue);
            return false;
        }


    }
}
